home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / areas.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  16KB  |  578 lines

  1. /*
  2.  *  AREAS.C
  3.  *
  4.  *  Written by jim nutt, John Dennis and released into the public domain
  5.  *  by John Dennis in 1994.
  6.  *
  7.  *  This file contains the routines to select one of the areas specified
  8.  *  in the config files.  It pops up a list with all the areas and lets
  9.  *  the user choose.
  10.  */
  11.  
  12. #include "msged.h"
  13. #include "main.h"
  14. #include "menu.h"
  15. #include "keys.h"
  16. #include "strextra.h"
  17. #include "memextra.h"
  18. #include "specch.h"
  19.  
  20. void areascan(void);
  21.  
  22. static char **alist;
  23. static char **alist2;
  24.  
  25. static void BuildList(char ***lst);
  26. static int AreaBox(char **Itms, int y1, int y2, int len, int def,
  27.                  WND * hPrev, WND * hWnd, int Sel, int Norm, int indent);
  28.  
  29. int mainArea(void)
  30. {
  31.     WND *hCurr, *hWnd;
  32.     int ret, wid, dep, i;
  33.  
  34.     msgederr = 0;
  35.     wid = maxx - 1;
  36.     dep = maxy - 2;
  37.  
  38.     hCurr = Wtop();
  39.  
  40.     hWnd = WndOpen(0, 1, wid, dep, NBDR, 0, cm[MN_BTXT]);
  41.  
  42.     WBox(0, 0, maxx - 1, maxy - 3, cm[MN_BTXT], SBDR);
  43.  
  44.     WWriteStr(3, 0, cm[LS_TTXT], "EchoID");
  45.     WWriteStr(maxx - 21, 0, cm[LS_TTXT], "Msgs");
  46.     WWriteStr(maxx - 13, 0, cm[LS_TTXT], "New");
  47.     WWriteStr(maxx - 7, 0, cm[LS_TTXT], "Last");
  48.  
  49.     BuildList(&alist);
  50.  
  51.     ret = AreaBox(alist, 1, min(dep - 2, SW->areas), wid - 1, SW->area, hCurr,
  52.                   hWnd, cm[MN_STXT], cm[MN_NTXT], 1);
  53.  
  54.     for (i = 0; i < SW->areas; i++)
  55.     {
  56.         xfree(alist[i]);
  57.     }
  58.  
  59.     xfree(alist);
  60.  
  61.     if (ret < 0)
  62.     {
  63.         msgederr = 1;
  64.     }
  65.  
  66.     WClose(hWnd);
  67.     WCurr(hCurr);
  68.     return ret;
  69. }
  70.  
  71. static void BuildList(char ***lst)
  72. {
  73.     int i;
  74.     AREA *a;
  75.     char line[181];
  76.     unsigned long unread, last;
  77.  
  78.     *lst = xcalloc(SW->areas + 2, sizeof(char *));
  79.  
  80.     for (i = 0; i < SW->areas; i++)
  81.     {
  82.         a = arealist + i;
  83.  
  84.         memset(line, ' ', sizeof line);
  85.         if (a->messages || SW->scanned)
  86.         {
  87.             last = a->lastread;
  88.             if (last > a->messages)
  89.             {
  90.                 last = a->messages;
  91.             }
  92.             unread = a->messages - a->lastread;
  93.             if (unread > a->messages)
  94.             {
  95.                 unread = a->messages;
  96.             }            
  97.             sprintf(line, "%c%-30.30s", unread ? SC14 : ' ', a->description);
  98.             line[strlen(line)] = ' ';
  99.             sprintf(line + maxx - 26, "%7lu%7lu%7lu",
  100.                     a->messages, unread, last);
  101.         }
  102.         else
  103.         {
  104.             sprintf(line, " %-30.30s", a->description);
  105.             line[strlen(line)] = ' ';
  106.             sprintf(line + maxx - 21, " -      -      -");
  107.         }
  108.  
  109.         (*lst)[i] = xstrdup(line);
  110.     }
  111.     (*lst)[i] = NULL;
  112. }
  113.  
  114. static void SelShowItem(char *text, int y, int len, int Attr, int indent)
  115. {
  116.     char line[256];
  117.     memset(line, ' ', 40);
  118.     strcpy(line + indent, text);
  119.     WPutsn(1, y, len, Attr, line);
  120. }
  121.  
  122. static void SelShowPage(char **text, int top, int bot, int len, int pos,
  123.                         int Attr, int indent)
  124. {
  125.     int i, y;
  126.     y = top;
  127.     for (i = pos; text[i] != NULL; i++)
  128.     {
  129.         if (y > bot)
  130.         {
  131.             break;
  132.         }
  133.  
  134.         SelShowItem(text[i], y++, len, Attr, indent);
  135.     }
  136.     if (y <= bot)
  137.     {
  138.         while (y <= bot)
  139.         {
  140.             SelShowItem(" ", y++, len, Attr, indent);
  141.         }
  142.     }
  143. }
  144.  
  145. static void CalcDef(int max, int cur, int *top, int miny, int maxy, int *y)
  146. {
  147.     int dif;
  148.     unused(cur);
  149.     dif = (maxy - 1) - miny;
  150.     if ((max - 1) - *top < dif && max > dif)
  151.     {
  152.         *y = maxy;
  153.         *top = (max - 1) - dif;
  154.     }
  155. }
  156.  
  157. static int AreaBox(char **Itms, int y1, int y2, int len, int def,
  158.                    WND * hPrev, WND * hWnd, int Sel, int Norm, int indent)
  159. {
  160.     EVT e;
  161.     char find[30];
  162.     int itemCnt, Stuff, done, curY, Msg, currItem, Top, page, i;
  163.  
  164.     itemCnt = 0;
  165.     Stuff = 0;
  166.     for (i = 0; Itms[i] != NULL; i++)
  167.     {
  168.         itemCnt++;
  169.     }
  170.  
  171.     currItem = def;
  172.     curY = y1;
  173.     page = y2 - y1;
  174.     Top = currItem;
  175.  
  176.     if (currItem + y1 < y1)
  177.     {
  178.         curY = y1 + currItem;
  179.         Top = 0;
  180.     }
  181.     else
  182.     {
  183.         if ((itemCnt - currItem) <= (y2 - y1))
  184.         {
  185.             Top -= ((y2 - y1 + 1) - (itemCnt - Top));
  186.             curY = y1 + (def - Top);
  187.             if (Top < 0)
  188.             {
  189.                 Top = 0;
  190.                 curY--;
  191.             }
  192.         }
  193.     }
  194.     done = 0;
  195.  
  196.     SelShowPage(Itms, y1, y2, len, Top, Norm, indent);
  197.     SelShowItem(Itms[currItem], curY, len, Sel, indent);
  198.  
  199.     TTClearQue();               /* clear input queue */
  200.  
  201.     memset(find, '\0', sizeof find);
  202.     while (!done)
  203.     {
  204.         if (!*find)
  205.         {
  206.             WCurr(hPrev);
  207.             WClear(16, 0, maxx - 36, 0, cm[CM_NINF]);
  208.             WWriteStr(0, 0, cm[MN_NTXT], ">>Pick New Area:");
  209.             WCurr(hWnd);
  210.         }
  211.  
  212.         if (!Stuff)
  213.         {
  214.             Msg = MnuGetMsg(&e, hWnd->wid);
  215.         }
  216.         else
  217.         {
  218.             e.msgtype = WM_CHAR;
  219.             Msg = Stuff;
  220.             Stuff = 0;
  221.         }
  222.  
  223.         switch (e.msgtype)
  224.         {
  225.         case WM_MOUSE:
  226.             switch (Msg)
  227.             {
  228.             case RMOU_CLCK:
  229.             case MOU_RBTUP:
  230.                 return -1;
  231.  
  232.             case LMOU_RPT:
  233.             case MOU_LBTDN:
  234.             case LMOU_CLCK:
  235.                 {
  236.                     int x, y;
  237.                     WndGetRel(e.x, e.y, &x, &y);
  238.                     if (y >= y1 && y <= y2)  /* in window */
  239.                     {
  240.                         Stuff = 0;
  241.                         if (x >= 0 && x < len)
  242.                         {
  243.                             if (y == curY)
  244.                             {
  245.                                 if (Msg == LMOU_CLCK || Msg == MOU_LBTUP)
  246.                                 {
  247.                                     return currItem;
  248.                                 }
  249.                                 else
  250.                                 {
  251.                                     continue;
  252.                                 }
  253.                             }
  254.  
  255.                             SelShowItem(Itms[currItem], curY, len, Norm, indent);
  256.  
  257.                             if (y > curY)
  258.                             {
  259.                                 currItem += y - curY;
  260.                             }
  261.                             else
  262.                             {
  263.                                 currItem -= curY - y;
  264.                             }
  265.  
  266.                             curY = y;
  267.                             SelShowItem(Itms[currItem], curY, len, Sel, indent);
  268.  
  269.                             if (Msg == LMOU_CLCK || Msg == MOU_LBTUP)
  270.                             {
  271.                                 return currItem;
  272.                             }
  273.                         }
  274.                     }
  275.                     else
  276.                     {
  277.                         if (Msg != LMOU_CLCK)
  278.                         {
  279.                             if (y < y1)
  280.                             {
  281.                                 Stuff = Key_Up;
  282.                             }
  283.                             else
  284.                             {
  285.                                 Stuff = Key_Dwn;
  286.                             }
  287.                         }
  288.                     }
  289.                 }
  290.                 memset(find, '\0', sizeof find);
  291.                 break;
  292.  
  293.             default:
  294.                 break;
  295.             }
  296.             break;
  297.  
  298.         case WM_CHAR:
  299.             switch (Msg)
  300.             {
  301.             case Key_Home:
  302.                 if (!currItem)
  303.                 {
  304.                     break;
  305.                 }
  306.                 SelShowItem(Itms[currItem], curY, len, Norm, indent);
  307.                 currItem = 0;
  308.                 Top = 0;
  309.                 curY = y1;
  310.                 SelShowPage(Itms, y1, y2, len, Top, Norm, indent);
  311.                 SelShowItem(Itms[currItem], curY, len, Sel, indent);
  312.                 memset(find, '\0', sizeof find);
  313.                 break;
  314.  
  315.             case Key_End:
  316.                 if (curr